1487D - Pythagorean Triples - CodeForces Solution


binary search brute force math number theory *1500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-funroll-all-loops,-fpeel-loops,-funswitch-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")

#define ll long long
#define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

const ll N = (ll)2e9 + 5, M = 500000;
const double PI = acos(-1);
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
const long long MOD = 1e9 + 7, MOD2 = 998244353, OO = 1e18;

ll t = 1;

void solve()
{
    ll n;
    cin >> n;
    if (n < 5)
    {
        return void(cout << "0\n");
    }
    ll l = 2, r = (n + 1) / 2;
    while (l < r)
    {
        ll md = (l + r + 1) >> 1;
        ll a = 2 * md - 1;
        ll b = a * a / 2;
        ll c = a * a - b;
        if (a <= b && b <= c && c <= n)l = md;
        else r = md - 1;
    }
    cout << l - 1 << '\n';
    return;
}

int main()
{    
    IO

    cin >> t;
    while (t--)
    {
        solve();
    }
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence